package com.learn.educourseboot.controller;

import com.learn.eduapi.entity.ResponseDTO;
import com.learn.eduapi.entity.course.Course;
import com.learn.eduapi.entity.util.RedisUtils;
import com.learn.educourseboot.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 课程
 */
@RestController
@CrossOrigin
@RequestMapping("course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 获取选课列表
     * @return
     */
    @GetMapping("getAllCourse/{nowPage}/{pageSize}")
    public  ResponseDTO<List<Course>> getAllCourse(@PathVariable("nowPage")Integer nowPage,
                                                   @PathVariable("pageSize")Integer pageSize) {
            ResponseDTO<List<Course>> responseDTO = new ResponseDTO<>();
            responseDTO.setContent(courseService.getAllCourse((nowPage-1)*pageSize,pageSize));
            responseDTO.setState(courseService.getAllCourseSize(0,0));
            responseDTO.setMessage("获取课程列表成功");
            return responseDTO;
    }


    /**
     * 获取已购课程列表
     * @return
     */
    @GetMapping("getMyCoursesById")
    public ResponseDTO<List<Course>> getMyCoursesById(@RequestParam("ids")String ids){
        String[] split = ids.split("&");
        System.out.println(Arrays.toString(split));
        String couresIds = split[0].substring(2);
        for (int i = 1; i < split.length; i++) {
            couresIds = couresIds+","+split[i].substring(2);
        }
        System.out.println("couresIds = " + couresIds);
        List<Course> courses = courseService.selectByIds(couresIds);

        ResponseDTO<List<Course>> responseDTO = new ResponseDTO<>();
        responseDTO.setContent(courses);
        responseDTO.setState(200);
        responseDTO.setMessage("成功获取已购课程");
        return responseDTO;
    }

    @GetMapping("getCrouseList")
    public ResponseDTO<List<Course>> getCrouseList(){
        ResponseDTO<List<Course>> responseDTO = new ResponseDTO<>();
        responseDTO.setContent(courseService.getCrouseList());
        return responseDTO;
    }
}
